<!DOCTYPE html>
<html>

<head>
    <title>个人中心</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <style>
        body {
            background-color: #66ccff;
        }

        .show {
            display: flex;
            justify-content: center;
        }

        .box {
            background-color: white;
            margin-top: 50px;
            width: 1000px;
            height: 600px;
        }

        .exit {
            display: flex;
            justify-content: center;
            margin-top: 10px;
        }

        .exit input {
            width: 300px;
            height: 50px;
            background-color: #ee0000;
            color: white;
            font-weight: bold;
            font-size: 15px;
            outline: none;
            border: none;
            cursor: pointer;
        }

        #create {
            margin-left: 45%;
            width: 100px;
            height: 50px;
            margin-top: 20px;
        }

        table tbody {
            display: block;
            max-height: 450px;
            overflow-x: hidden;
            scrollbar-width: 0;
        }

        table tbody::-webkit-scrollbar {
            display: none;
        }

        table thead,
        tbody tr {
            display: table;
            width: 90%;
            table-layout: fixed;
            margin: auto;
        }
    </style>
    <script src="../../layui/layui.js" type="text/javascript"></script>
    <script src="../../jquery-3.6.0.min.js" type="text/javascript"></script>
    <script>
        window.onload = function () {
            $.ajax({
                url: "check.php",
                async: false,
                success(res) {
                    if (res != 1) {
                        location.replace("../../404.html");
                    } else {
                        getData();
                    }
                }
            })
            $.ajax({
                url: "../../type/getFile.php",
                success: function (res) {
                    let select = document.getElementById("acticle");
                    for (let i = 0; i < res.length; i++) {
                        let option = document.createElement("option");
                        option.setAttribute("value", res[i].title);
                        option.innerText = res[i].title;
                        select.appendChild(option);
                    }
                    layui.use('form', function () {
                        let form = layui.form;
                        form.render('select');
                    })
                }
            })
        }

        function getData() {
            $.ajax({
                url: "getData.php",
                success(res) {
                    let tbody = document.getElementById("user");
                    for (let i = 0; i < res[0].length; i++) {
                        let tr = document.createElement("tr");
                        for (let j = 0; j < 2; j++) {
                            let td = document.createElement("td");
                            let user = document.createTextNode(res[0][i][j]);
                            if (j == 0) td.setAttribute("style", "width: 100px;");
                            td.appendChild(user);
                            tr.appendChild(td);
                        }
                        tbody.appendChild(tr);
                    }

                    tbody = document.getElementById("data");
                    for (let i = 0; i < res[1].length; i++) {
                        let tr = document.createElement("tr");
                        for (let j = 0; j < 8; j++) {
                            let td = document.createElement("td");
                            let data = document.createTextNode(res[1][i][j]);
                            td.appendChild(data);
                            tr.appendChild(td);
                        }
                        tbody.appendChild(tr);
                    }
                }
            })
        }

        function creatUser() {
            let username = prompt("请输入用户名");
            if (username) {
                $.ajax({
                    url: "createUser.php",
                    type: "POST",
                    data: {
                        account: username
                    },
                    success(res) {
                        if (res == 1) {
                            alert("用户已存在！");
                            return
                        }
                        window.location.reload();
                        alert("创建用户成功！\n初始密码为123456");
                    }
                })
            } else if (username == "") {
                alert("请输入用户名！");
            }
        }

        function sixRandom() {
            var rand = "";
            for (let i = 0; i < 6; i++) {
                rand += Math.floor(Math.random() * 10);
            }
            return rand;
        }

        function getRandom() {
            var rand = sixRandom();
            while (rand[0] == 0) rand = sixRandom();
            $("#rand").val(rand);
        }

        function exit() {
            $.ajax({
                url: '../deleteSession.php',
                success(res) {
                    window.location.replace("../login.html");
                }
            });
        }
    </script>
</head>

<body>
    <div class="show">
        <div class="box" id="personalCenter">
            <div class="layui-tab layui-tab-brief">
                <ul class="layui-tab-title">
                    <li class="layui-this">管理用户</li>
                    <li>查看数据</li>
                    <li>新增文章</li>
                    <li>发布竞赛</li>
                    <li>文章处理</li>
                    <li onclick="window.location.href = '../../gameInfo/game.html'">查看竞赛</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <table class="layui-table table">
                            <thead>
                                <tr>
                                    <th style="width: 100px;">ID</th>
                                    <th>用户名</th>
                                </tr>
                            </thead>
                            <tbody id="user" style="max-height: 380px;"></tbody>
                        </table>
                        <input id="create" class="layui-btn" type="button" value="创建用户" onclick="creatUser()">
                    </div>
                    <div class="layui-tab-item">
                        <table class="layui-table table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>文章</th>
                                    <th>字数</th>
                                    <th>时间</th>
                                    <th>日期</th>
                                    <th>速度</th>
                                    <th>正确率</th>
                                </tr>
                            </thead>
                            <tbody id="data"></tbody>
                        </table>
                    </div>
                    <div class="layui-tab-item">
                        <form class="layui-form" action="upload.php" method="post" target="_blank">
                            <div class="layui-form-item">
                                <label class="layui-form-label">标题</label>
                                <div class="layui-input-block" style="width: 80%;">
                                    <input type="text" name="title" required lay-verify="required" placeholder="请输入标题"
                                        autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">内容</label>
                                <div class="layui-input-block" style="width: 80%;">
                                    <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label"></label>
                                <div class="layui-word-aux">
                                    <span>请确保文章内容不含任何特殊字符，中文文章不含英文字符，可使用本站提供程序进行粗略处理，或者手动处理</span>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="acticle">上传</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="layui-tab-item">
                        <form class="layui-form" action="createGame.php" method="post" target="returnTip">
                            <div class="layui-form-item">
                                <label class="layui-form-label">邀请码</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="code" id="rand" required lay-verify="required" readonly
                                        class="layui-input">
                                </div>
                                <input class="layui-btn" type="button" onclick="getRandom()" value="生成邀请码">
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">文章</label>
                                <div class="layui-input-inline">
                                    <select id="acticle" name="acticle" required lay-verify="required" lay-search>
                                        <option value=""></option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">结束时间</label>
                                <div class="layui-inline">
                                    <input name="date" required autocomplete="off" class="layui-input" id="date">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">限时模式</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="switch" lay-skin="switch">
                                </div>
                            </div>
                            <div class="layui-form-item" id="limit" style="display: none;">
                                <label class="layui-form-label">限时</label>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <input type="number" name="limitTime" class="layui-input" min="1">
                                </div>
                                <div class="layui-form-mid layui-word-aux">分钟</div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                        </form>
                        <iframe src="" frameborder="0" name="returnTip"></iframe>
                    </div>
                    <div class="layui-tab-item">
                        <iframe width="100%" height="500px" frameborder="0" src="replace.html"></iframe>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="exit">
        <input type="button" value="退出登录" onclick="exit()">
    </div>
    <script>
        layui.use('form', function () {
            var form = layui.form;

            form.on('switch', function (data) {
                if (data.elem.checked) $('#limit').show();
                else $('#limit').hide();
            })
        })

        layui.use('laydate', function () {
            let laydate = layui.laydate;

            laydate.render({
                elem: '#date'
                , type: 'datetime'
                , min: Date.now() + 60000
            });
        });
    </script>
</body>

</html>